//
//  CellOfMostPopularCommendScenicSpotView.m
//  FreeTravel
//
//  Created by 徐康 on 16/9/3.
//  Copyright © 2016年 KX. All rights reserved.
#import "WebViewOfCommendViewController.h"
#import "FreeTravelMacro.h"
#import "CellOfCellOfMostPopularCommendScenicSpotView.h"
#import "CellOfMostPopularCommendScenicSpotView.h"
#import "CityViewController.h"
#import "ModelOfMostPopularCommendScenicSpotView.h"
@interface CellOfMostPopularCommendScenicSpotView ()<UICollectionViewDelegate,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@end
@implementation CellOfMostPopularCommendScenicSpotView
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createCollectionView];
    }
    return self;
}
- (void)createCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    layout.itemSize = CGSizeMake((SCREEN_WIDTH-40)/3,(SCREEN_WIDTH-40)/3+20+(SCREEN_WIDTH-40)/3/9);
    [self.contentView addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[CellOfCellOfMostPopularCommendScenicSpotView class] forCellWithReuseIdentifier:@"CellOfCellOfMostPopularCommendScenicSpotView"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CellOfMostPopularCommendScenicSpotViewHead"];
}
- (void)layoutSubviews{
    [super layoutSubviews];
    self.collectionView.frame = self.contentView.bounds;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSLog(@"%ld",self.model.most_popular.count);
    return self.model.most_popular.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CellOfCellOfMostPopularCommendScenicSpotView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellOfCellOfMostPopularCommendScenicSpotView" forIndexPath:indexPath];
    cell.model = self.model.most_popular[indexPath.row];
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
        UICollectionReusableView *head = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CellOfMostPopularCommendScenicSpotViewHead" forIndexPath:indexPath];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
        label.text = @"最受欢迎";
        label.textAlignment = NSTextAlignmentCenter;
    
        label.font =  [UIFont fontWithName:@"Helvetica-Bold" size:15.f];
    [head addSubview:label];
        return head;

    
    
   }
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ModelOfMostPopularCommendScenicSpotView *model = self.model.most_popular[indexPath.row];
    WebViewOfCommendViewController *webVC = [[WebViewOfCommendViewController alloc]init];
    webVC.url = [NSString stringWithFormat:@"http://place.qyer.com/barcelona/event/%@/?cate=151&from_device=app",model.ID];
    CityViewController *cityVC = (CityViewController *)self.nextResponder.nextResponder.nextResponder.nextResponder;
    [cityVC.navigationController pushViewController:webVC animated:YES];
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(SCREEN_WIDTH, 50);
}
- (void)setModel:(ModelOfDataCommendScenicSpotView *)model{
    _model = model;
    [self.collectionView reloadData];
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
